package HandleDB;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class MySqlConnection {

	private Connection conn = null;
	private String userName = null;
	private String password = null;
	private  PreparedStatement ps = null;
	private FileInputStream fis = null;


	public void connect() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (Exception ex) {/* handle the error */
		}

		try {
				System.out.println("Connecting to database...");
				conn = DriverManager.getConnection("jdbc:mysql://localhost/sgi", userName, password);
			    System.out.println("SQL connection succeed");

		} catch (SQLException ex) {
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		}
	}

	// Gets values from database through ResultSet using specific query
	public ResultSet getFromDB(ResultSet rs, String query) {
		try {
			PreparedStatement stmt = conn.prepareStatement(query);
			rs = stmt.executeQuery(query);
			return rs;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	// Updates the database using specific query
	public void updateDB(String query) {
		try {
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
	